package com.stylefeng.guns.core.shiro;

import com.stylefeng.guns.core.shiro.factory.IShiro;
import com.stylefeng.guns.core.shiro.factory.ShiroFactroy;
import com.stylefeng.guns.core.util.SpringContextHolder;
import com.stylefeng.guns.modular.system.dao.UserMapper;
import com.stylefeng.guns.modular.system.model.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.realm.Realm;

public class WxCodeRealm implements Realm {

    @Override
    public String getName() {
        return "wxCodeRealm";
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof WxCodeAuthcToken;
    }

    @Override
    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
        UserMapper userMapper = SpringContextHolder.getBean(UserMapper.class);
        IShiro shiroFactory = ShiroFactroy.me();
        WxCodeAuthcToken token = (WxCodeAuthcToken) authcToken;
        String code = (String) token.getPrincipal();
        User user = userMapper.selectById(Integer.parseInt(code));
        ShiroUser shiroUser = shiroFactory.shiroUser(user);
        return new SimpleAuthenticationInfo(shiroUser, "1", getName());
    }



}
